package com.felix.algorithms.planning;

/**
 * NumArray
 *
 * @author xuejinhua
 * @date 2018/9/11 13:58
 */
public class NumArray {
    private int[] sums;

    public NumArray(int[] nums) {
        this.sums = nums;
        for (int i = 1; i < nums.length; ++i) {
            sums[i] += sums[i - 1];
        }
    }

    public int sumRange(int i, int j) {
        return i == 0 ? sums[j] : sums[j] - sums[i];
    }

    public static void main(String[] args) {
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray obj = new NumArray(nums);
        int param_1 = obj.sumRange(0, 2);
        System.out.println(param_1);
    }
}
